<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_menu} function plugin
     *
     * Type:     function
     * Name:     load_menu
     * Purpose:  Load the menu into a predefined variable
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * optional template parameters:
     *
     *   * var (string) -- name of variable where to load the
     *                     data tree to
     *                     >> default: menu
     *
     *   * depth (int)  -- maximum depth to go to
     *                     >> default: 2
     *
     *   * node (int)   -- folder to start loading menu from
     *                     >> default: site root
     *
     *   * pages (bool) -- whether to load separate pages from
     *                     the tree
     *                     >> default: false
     *
     * @return string
     */
    function smarty_function_load_menu($params, &$smarty){
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'menu';
        }else{
            $var = $params['var'];
        }

        //depth of tree
        if(empty($params['depth'])){
            $depth = 2;
        }else{
            $depth = (int)$params['depth'];
        }

        //node to start from
        if(empty($params['node'])){
            $node = null;
        }else{
            $node = (int)$params['node'];
        }

        if($_GET['gonz']) $db->debug = 1;

        //node to start from
        if(empty($params['type'])){
            $type = null;
        }else{
            $type = (string)$params['type'];
            if(!$type = $db->getOne("SELECT id FROM datatype WHERE codeset = '".$type."'")){

            }
        }

        //get the folders?
        /*
        if(!empty($params['folders'])){
            if($params['folders'] == 'true'){
                $folders = true;
            }else{
                $folders = false;
            }
        }else{
            $folders = true;
        }
        */

        //get the pages?
        if(!empty($params['pages'])){
            if($params['pages'] == 'true'){
                $pages = true;
            }else{
                $pages = false;
            }
        }else{
            $pages = false;
        }


        //get the folder tree
        $tree = new tree($db,'structure AS s LEFT JOIN objectmap AS om ON s.id = om.nodeid',null,'s.id AS id, s.level AS level, get_path(id) as path, om.title AS title, om.published_pages AS published_pages');
        $data = $tree->getTree($node,$depth,false,'version = 1 AND wf_state = 1 '.($type?' AND s.node_type = '.$type:''));

        //now, see if we need to fetch the pages as well
        if($pages){
            if($node){
              $rootNodeID = $node;
            }else{
              //get the id of the root node
              $rootNodeID = (int)$db->getOne("SELECT id FROM structure WHERE parent_id IS NULL");
            }

            //first, get the list of published pages under the root node
            $menuPages  = $db->getAll("SELECT
                                            id, pageurl, title, get_path(nodeid)||pageurl||'.html' AS url
                                        FROM pagemap
                                        WHERE nodeid = ".$rootNodeID." AND version = 1 AND wf_state = 1
                                        ORDER BY ".(!$params['order']?'title':$params['order']));

            $menu = array_merge((array)$data,$menuPages);
        }else{
            $menu = $data;
        }

        $smarty->assign($var,$menu);
    }
?>